<?php

namespace Models;
use Phalcon\Db\RawValue;

class Cache extends BaseModel {


    public function beforeValidationOnCreate()
    {
        if (!isset($this->cvalue) || !$this->cvalue) {
            $this->cvalue = new RawValue('default');
        }
    }

    public function beforeValidationOnUpdate()
    {
        if (!isset($this->cvalue) || !$this->cvalue) {
            $this->cvalue = new RawValue('default');
        }
    }

    public function notSave()
    {
        //Obtain the flash service from the DI container
        $flash = $this->getDI()->getFlash();

        //Show validation messages
        foreach ($this->getMessages() as $message) {
            $flash->error($message);
        }
    }

    public function beforeUpdate()
    {
        $this->cvalue = serialize($this->cvalue);
    }

    public function afterFetch()
    {
        $this->cvalue = unserialize($this->cvalue);
    }    

}